﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.DataAccess;

namespace University.Workshop.Presentation
{
    /// <summary>
    /// Interaction logic for WelcomePage.xaml
    /// </summary>
    public partial class Login : Page
    {
        MainWindow mw;
        public Login(MainWindow mW)
        {
            mw = mW;
            InitializeComponent();
            txtUser.Focus();
        }



        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        private void PasswordTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((PasswordBox)sender).Password + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsLetterOrDigit(ch) || (ch.Equals('_')));
            }
        }

        private void PersonNamesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsLetterOrDigit(ch) || (ch.Equals('_')) );
            }
        }

        private void PersonNamesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            if (e.Key == Key.Enter)
                txtPass.Focus();
        }

        private void PasswordTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            if (e.Key == Key.Enter)
                btnIngresar_Click(sender, e);
        }

        private void btnIngresar_Click(object sender, RoutedEventArgs e)
        {
            ICollection<Model.AppConfiguration> config = DataAccess.DataAccessConfiguration.GetCurrent();
            if (config.Count != 0)
            {
                Model.AppConfiguration c = config.First();
                if (c.FlagSync)
                {
                    DataAccess.Generator.GenerateWaterReadingsAndWaterCollectsForAllKardex();
                    DataAccess.Generator.GenerateFineForWaterCollects();
                }
            }
            string login = txtUser.Text;
            string password = txtPass.Password;

            Model.User user = new Model.User() { Login = login, Password = password };

            var persons = DataAccessUser.GetUsersByLoginAndPassword(user);
            if (persons.Count == 0)
                MessageBox.Show("Usuario y/o contraseñas inválidas, intente de nuevo.");
            else
            {
                
                mw.verificarMultas();

                this.Visibility = System.Windows.Visibility.Hidden;

                if (login.Equals(password) && !(login.Equals("admin")))
                    mw.cambiarPassword(user);
                else
                {
                    mw.mostrarMenus(user);
                    user = null;

                    login = string.Empty;
                    password = string.Empty;

                    txtUser.Text = string.Empty;
                    txtPass.Password = string.Empty;
                }
            }

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Login";
        }

        
        //private void VerifyCurrentManangment()
        //{
        //    //---------------------------Generate readings---------------------------------------//
        //    string managementSaved = System.Configuration.ConfigurationSettings.AppSettings["CurrentManagement"];
        //    string passExpirationSaved = System.Configuration.ConfigurationSettings.AppSettings["PassExpirationDate"];
        //    string currentManagement = DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
        //    if (managementSaved == string.Empty)
        //    {
        //        bool realizable = DataAccess.Generator.GenerateReadingAndCollects();
        //        if (!realizable)
        //            MessageBox.Show("Debe Registar tarifas y multas");
        //        else
        //        {
        //            System.Configuration.ConfigurationSettings.AppSettings["CurrentManagement"] = currentManagement;
        //            System.Configuration.ConfigurationSettings.AppSettings["PassExpirationDate"] = "false";
        //        }
        //    }
        //    else
        //    {
        //        if (managementSaved != currentManagement)
        //        {
        //            bool realizable = DataAccess.Generator.GenerateReadingAndCollects();
        //            if (!realizable)
        //                MessageBox.Show("Debe Registar tarifas y multas");
        //            else
        //            {
        //                System.Configuration.ConfigurationSettings.AppSettings["CurrentManagement"] = currentManagement;
        //                System.Configuration.ConfigurationSettings.AppSettings["PassExpirationDate"] = "false";
        //            }
        //        }
        //    }
            
        //    //--------------------------------generate fines----------------------------------//
        //    ICollection<Model.OtbData> otbData = University.Workshop.DataAccess.DataAccessOtbData.GetCurrentOtbData();
        //    if (otbData.Count > 0)
        //    {
        //        if (passExpirationSaved == "false" && currentManagement == managementSaved && DateTime.Now.Day > otbData.First().ExpirationDay)
        //        {
        //            bool realizable = DataAccess.Generator.GenerateCutsAndFines();
        //            System.Configuration.ConfigurationSettings.AppSettings["PassExpirationDate"] = "true";
        //        }
        //    }
        //    else
        //    {
        //        MessageBox.Show("Debe registrar datos de OTB");
        //    }
        //}
    }
}
